package com.pointi.emoBasedataM;

import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.pointi.emoTools.* ;

public class ThreadScheduler extends Thread
{
	//____________________________________________________________________________________________________
	// DEF

	ConcurrentLinkedQueue<ThreadScheduler> manager ;
	ConfigurationData conf_data ;
	JDBC jdbc ;

	Date log_date ;
	Date run_date ;

	//____________________________________________________________________________________________________
	// INIT

	public ThreadScheduler(ConcurrentLinkedQueue<ThreadScheduler> manager)
	{
		this.manager = manager ;
	}
	public void start(ConfigurationData conf_data, JDBC jdbc)
	{
		log_date = new Date() ;
		run_date = new Date() ;
		this.conf_data = conf_data ;
		this.jdbc = jdbc ;
		this.manager.add(this) ;
		super.start() ;
	}

	//____________________________________________________________________________________________________
	// RUN

	public void run()
	{
		this.fnWorkScheduler() ;
		this.manager.remove(this) ;
	}

	//____________________________________________________________________________________________________
	// Basedata v3.0 // Work Scheduler // 2014.07.03

	private void fnWorkScheduler()
	{
		// Emotion Engine Configuration
		emoConfig eCONF = new emoConfig() ;
		jdbc.fnGetSysParamEngineConfig(eCONF) ;

		// Target List Container
		ArrayList<String> targetList = new ArrayList<String>() ;

		// Worker Thread
		ConcurrentLinkedQueue<ThreadWork> worker = new ConcurrentLinkedQueue<ThreadWork>() ;

		int workerLimit = Integer.valueOf(conf_data.s_worker_limit) ;
		int workerCount = 0 ;

		try {
			boolean loop = true ;
			while(loop) {
				if (ezDate.isDateChange(log_date)) { // [T] NEW DATE [F] TODAY
					log_date = new Date() ;
					jdbc.fnPrepareDatabase() ;
					this.out(ezDate.toString(ezDate.fnGetYesterday(log_date), ezDate.YYYYMMDD_HHMMSS_BAR)) ;
				}
				if (this.isUnhandledList(targetList)) { // Build Target List
					boolean loop_in = true ;
					while(loop_in) {
						if (worker.isEmpty()) {
							worker = new ConcurrentLinkedQueue<ThreadWork>() ;
						}
						workerCount = worker.size() ;
						if (workerCount < workerLimit) {
							new ThreadWork(worker).start(jdbc, eCONF, targetList.get(0), run_date) ;
							targetList.remove(0) ;
						}
						if (targetList.isEmpty()) { break ; }
					}
				}
				Thread.sleep(Integer.valueOf(conf_data.s_check_interval)) ;
			}
		}
		catch(Exception e) { e.printStackTrace() ; }
	}

	//____________________________________________________________________________________________________
	// TOOLS

	private boolean isUnhandledList(ArrayList<String> targetList)
	{
		run_date = jdbc.fnCheckUnhandledList(log_date) ;
		if (run_date == null) {
			// jdbc.fnDropEngineTable(_Date) ; // NOTE
			this.out("[Unhandled List][Null]") ;
			return false ;
		} else {
			this.out(ezDate.toString(run_date, ezDate.YYYYMMDD_HHMMSS_BAR)) ;
			if(targetList.size()==0) { jdbc.fnGetUnhandledList(run_date, targetList) ; }
			if(targetList.size()==0) { return false ; }
			return true ;
		}
	}

	private void out(Object obj)
	{
		System.out.println(obj) ;
	}

	//____________________________________________________________________________________________________
	// EOC
}
